#!/bin/bash

# service_manager.sh

SERVICE_NAME="httpd"

usage() {
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
}

# 参数数量
if [ "$#" -ne 1 ]; then
    usage
fi

# 根据第一个参数执行相应的服务管理操作
case "$1" in
    start)
        echo "Starting $SERVICE_NAME..."
        sudo systemctl start $SERVICE_NAME
        if sudo systemctl is-active --quiet $SERVICE_NAME; then
            echo "$SERVICE_NAME started successfully."
        else
            echo "Failed to start $SERVICE_NAME."
        fi
        ;;
    stop)
        echo "Stopping $SERVICE_NAME..."
        sudo systemctl stop $SERVICE_NAME
        if sudo systemctl is-active --quiet $SERVICE_NAME; then
            echo "Failed to stop $SERVICE_NAME."
        else
            echo "$SERVICE_NAME stopped successfully."
        fi
        ;;
    restart)
        echo "Restarting $SERVICE_NAME..."
        sudo systemctl restart $SERVICE_NAME
        if sudo systemctl is-active --quiet $SERVICE_NAME; then
            echo "$SERVICE_NAME restarted successfully."
        else
            echo "Failed to restart $SERVICE_NAME."
        fi
        ;;
    status)
        echo "Checking the status of $SERVICE_NAME..."
        sudo systemctl status $SERVICE_NAME
        ;;
    *)
        usage
        ;;
esac